package org.mtdownload.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.AbstractAction;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.KeyStroke;
import javax.swing.WindowConstants;

import org.mtdownload.download.DownloadTask;

public class AddDownloadDialog extends JDialog {

	private static final long serialVersionUID = 3833184748422901815L;
	
	private JPanel contentPane=null;
	private boolean cancelled=true;
	private DownloadInfoPanel downloadInfoPanel=null;
	private AlternateDownloadUrlsPanel alternateDownloadUrlsPanel=new AlternateDownloadUrlsPanel();
	private boolean loadUrlsFromFile=false;
	
	public AddDownloadDialog(Frame owner, String title, boolean modal,boolean loadUrlsFromFile)
			throws HeadlessException {
		super(owner, title, modal);
		this.loadUrlsFromFile=loadUrlsFromFile;
		init();
	}
	
	private void init(){
		setSize(450,400);
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);		
		contentPane=(JPanel)getContentPane();
		contentPane.setLayout(new BorderLayout());
		InputMap inputMap=contentPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
		inputMap.put(KeyStroke.getKeyStroke("ESCAPE"),"close");
		ActionMap actionMap=contentPane.getActionMap();
		actionMap.put("close",new AbstractAction(){
			private static final long serialVersionUID = 3689345520585750328L;
			public void actionPerformed(ActionEvent e) {
				cancelled=true;
				dispose();								
			}
		});
		
		downloadInfoPanel=new DownloadInfoPanel(loadUrlsFromFile);
		JTabbedPane tab=new JTabbedPane();
		tab.addTab("General",downloadInfoPanel);
		if(!loadUrlsFromFile){
			tab.addTab("Alternate Url's",alternateDownloadUrlsPanel);
		}
		contentPane.add(tab,BorderLayout.CENTER);
		
		JPanel buttonsPanel=new JPanel(new FlowLayout(FlowLayout.RIGHT));
		JButton okButton=new JButton("OK");
		okButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				cancelled=false;
				if(!loadUrlsFromFile){
					File destinationFile=new File(downloadInfoPanel.getDestinationFile());
					if(destinationFile.exists()){
						int result=JOptionPane.showConfirmDialog(AddDownloadDialog.this,"Destination file exists. Do you want to overwrite it?","Confirm",JOptionPane.YES_NO_OPTION);
						if(result!=JOptionPane.YES_OPTION){
							return;						
						}										
					}						
				}
				dispose();				
			}			
		});
		JButton cancelButton=new JButton("Cancel");
		cancelButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				cancelled=true;
				dispose();				
			}			
		});
		buttonsPanel.add(okButton);
		buttonsPanel.add(cancelButton);
		getRootPane().setDefaultButton(okButton);
		contentPane.add(buttonsPanel,BorderLayout.SOUTH);
	}
	
	public DownloadTask getDownloadTask(){
		if(cancelled){
			return null;
		}
		DownloadTask task=new DownloadTask();
		task.setAlternateURLs(alternateDownloadUrlsPanel.getAlternateUrls());
		task.setComplete(false);
		task.setPassword(downloadInfoPanel.getPassword());		
		task.setUrl(downloadInfoPanel.getURL());
		task.setDestinationFileName(downloadInfoPanel.getDestinationFile());
		task.setDestinationDirectory(downloadInfoPanel.getSaveToDir());
		task.setThreadNum(downloadInfoPanel.getSplitFileNumber());
		return task;
	}
}
